using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using CoachingOrganizer.Logic.Models.Common;
using CoachingOrganizer.ClientAdapters.Android;
using CoachingOrganizer.Android.Common;
using Android.Preferences;

namespace CoachingOrganizer.Android.Mediators
{
    public class ModelsMediator
    {
        private readonly Lazy<InteractionModel> _model = new Lazy<InteractionModel>(() => GetInteractionModel(), true);
        private static readonly Lazy<ModelsMediator> s_instance = new Lazy<ModelsMediator>(() => new ModelsMediator(), true);

        public static ModelsMediator Instance
        {
            get { return s_instance.Value; }
        }

        public InteractionModel Model
        {
            get { return _model.Value; }
        }

        static ModelsMediator()
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        }

        private static InteractionModel GetInteractionModel()
        {
            return new InteractionModel(new AndroidLogicExtender(PreferenceManager.GetDefaultSharedPreferences(Application.Context)));
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            global::Android.Util.Log.Wtf("CoachingOrganizer", Java.Lang.Throwable.FromException((Exception)e.ExceptionObject));
        }
    }
}